package cn.sumpu.app.wlcity.biz.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.wlcity.biz.SeckillBiz;
import cn.sumpu.app.wlcity.biz.SmsBiz;
import cn.sumpu.app.wlcity.dao.CodeDao;
import cn.sumpu.app.wlcity.dao.SeckillDao;
import cn.sumpu.app.wlcity.domain.Code;
import cn.sumpu.app.wlcity.domain.Seckill;
import cn.sumpu.app.wlcity.domain.SeckillPhone;
import cn.sumpu.app.wlcity.domain.SeckillTime;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;

public class SeckillBizImpl implements SeckillBiz {

	private static Logger logger = LoggerFactory
			.getLogger(SeckillBizImpl.class);
	
	private SeckillDao seckillDao;
	
	private CodeDao codeDao;
	
	private SmsBiz smsBiz;
	
	public void setSmsBiz(SmsBiz smsBiz) {
		this.smsBiz = smsBiz;
	}
	
	public void setSeckillDao(SeckillDao seckillDao) {
		this.seckillDao = seckillDao;
	}
	
	
	public void setCodeDao(CodeDao codeDao) {
		this.codeDao = codeDao;
	}

	/*查询所有的秒杀活动*/
	@Override
	public List<Seckill> getSeckillList(Integer page,String seckillType) throws AppBusinessException {
		
		int pageSize=Common.PAGE_SIZE_SECKILL;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page-1)*pageSize);
		map.put("pageSize", pageSize);
		map.put("seckillType", seckillType);
		return seckillDao.getSeckillList(map);  
	}
	/*查询所有的参加活动的用户*/
	@Override
	public List<SeckillPhone> getSeckillPhoneList(Integer pageUser,String seckillId) throws AppBusinessException {
		
		int pageSize=Common.PAGE_SIZE_SECKILL;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (pageUser-1)*pageSize);
		map.put("pageSize", pageSize);
		map.put("seckillId", seckillId);
		return seckillDao.getSeckillPhoneList(map);  
	}
	/*插入秒杀活动的联系方式*/
	@Override
	public void insertSeckillPhone(SeckillPhone seckillPhone)throws AppBusinessException {
		seckillDao.insertSeckillPhone(seckillPhone);
	}
	
	/*插入秒杀活动的时间*/
	@Override
	public void insertSeckillTime(SeckillTime seckillTime)throws AppBusinessException {
		seckillDao.insertSeckillTime(seckillTime);
	}
	/*插入秒杀活动信息*/
	@Override
	public void insertSeckill(Seckill seckill)throws AppBusinessException {
		seckillDao.insert(seckill);
	}
	/*查询秒杀活动的总数*/
	@Override
	public int getSeckillListCount(String seckillType) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("seckillType", seckillType);
		return seckillDao.getSeckillListCount(map);
	}
   /*查询参与用户的总数*/
	@Override
	public int getSeckillPhoneListCount(String seckillId) throws AppBusinessException {
		return seckillDao.getSeckillPhoneListCount(seckillId);
	}
	 
    /*	删除此活动*/
	@Override
	public void deleteSeckill(String identifier) throws AppBusinessException {
		seckillDao.deleteSeckill(identifier);

	}
	/*删除活动时间*/
	@Override
	public void deleteSeckillTime(String seckillId) throws AppBusinessException {
		seckillDao.deleteSeckillTime(seckillId);

	}
	/*查询某单个秒杀活动*/
	@Override
	public Seckill selectSeckill(String seckillId) throws AppBusinessException {
		return seckillDao.selectSeckill(seckillId);

	}
	/*更新秒杀活动*/
	@Override
	public void updateSeckill(Seckill seckill) throws AppBusinessException {
		 seckillDao.updateSeckill(seckill);

	}
	/*查询所有的10个秒杀活动*/
	@Override
	public List<Seckill> getSeckillListFive(Integer page,String seckillType) throws AppBusinessException {
		
		int pageSize=Common.PAGE_SIZE_SECKILLFIVE;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page-1)*pageSize);
		map.put("pageSize", pageSize);
		map.put("seckillType", seckillType);
		return seckillDao.getSeckillListFive(map);  
	}
	//判断秒杀事务处理
	@Override
	public String seckill(String seckillId,String phoneNum)throws AppBusinessException {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("seckillId", seckillId);
		map.put("phoneNum", phoneNum);
		SeckillPhone seckillPhoneOld=seckillDao.selectSeckillPhoneOld(map);
		if(seckillPhoneOld!=null){
			//已参加过活动
			return "error";
		}
		else {
			Seckill seckill = seckillDao.selectSeckill(seckillId);
			SeckillTime seckillTime=null;
			if(seckill.getIsDaily()==true){
				seckillTime =seckillDao.getTryLuckTime(seckillId);
				}
			else{
				seckillTime =seckillDao.getSeckillTime(seckillId);
			}
			
		if(seckillTime!=null&&seckillTime.getCount()>0){
			String validateMsg = smsBiz.send(phoneNum, seckill.getValidateMsg());
			SeckillPhone seckillPhone =new SeckillPhone();
			seckillPhone.setPhoneNumber(phoneNum);
			seckillPhone.setSeckillId(seckillId);
//			if ("TRUE".equals(validateMsg)) {
				//秒杀成功
				int count =seckillTime.getCount()-1;
				seckillTime.setCount(count);
				seckillDao.updateSeckillTime(seckillTime);
				seckillPhone.setStatus("02");
				seckillDao.insertSeckillPhone(seckillPhone);
				return "success";
//			} else {
//				seckillPhone.setStatus("03");
//				seckillDao.insertSeckillPhone(seckillPhone);
//				return "fail";
//			}
		}else {
			//秒杀失败
			return "fail";
		}
		}
	}
	//查询活动是否开始
	@Override
	public Boolean isSeckillOver(String seckillId)throws AppBusinessException {
		
		try {
			Seckill seckillDate =seckillDao.selectSeckill(seckillId);
			//A判断是否日常活动
			if(null != seckillDate && seckillDate.getIsDaily() != null && seckillDate.getIsDaily() == true){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
				Date endDate = sdf.parse(seckillDate.getEndDate());
				Date startDate =sdf.parse(seckillDate.getStartDate());
				Date nowDate = Calendar.getInstance().getTime();    
				Boolean dateIsEnd =nowDate.before(endDate);
				Boolean dateIsStart =nowDate.after(startDate);
				//B日常活动时间段为真
				if(dateIsEnd==true && dateIsStart==true){
					SeckillTime seckillTimeIsNull =seckillDao.getTryLuckTime(seckillId);
					//C如果分时为空
					if(seckillTimeIsNull==null){
						return false;
					}
					//C分时段为真
					else{
						return true;
					}
				}
				//B日常时间段为假
				else{
					return false;
				}
				
			}
			//A不是日常活动
			else{
				SeckillTime seckillTimeIsNull =seckillDao.getSeckillTime(seckillId);
				if(seckillTimeIsNull==null){
					return false;
				}else{
					return true;
				}
			}
			
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		return false;
		
	}

	@Override
	public void initCount() {
		/*System.out.println("xxxxxxxxxxxx");*/
		try {
			seckillDao.updateCount();
		} catch (AppBusinessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("initCount fail!",e);
			}
		}
	}

	@Override
	public Code getSkillTry() throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("type", "try_rule");
		map.put("value", "01");
		List<Code> codeList = codeDao.getCodeList(map);
		if(codeList != null && codeList.size() > 0){
			return codeList.get(0);
		}
		return null;
	}

	@Override
	public void updateSkillTry(String content) throws AppBusinessException {
		Code code = new Code();
		code.setType("try_rule");
		code.setValue("01");
		code.setSort(0);
		code.setLabel(content);
		codeDao.updateCode(code);
	}
	
	
	

}
